package company.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import company.common.R;
import company.domain.Announcements;
import company.domain.Rescuestations;
import company.domain.Science;
import company.service.ScienceService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.List;

@RestController
public class ScienceController {
    @Resource
    ScienceService scienceService;

    /*
     * 查询科普
     * */
    @RequestMapping("/selectScienceAll")
    public R selectScienceAll(){
        List<Science> list = this.scienceService.list();
        return new R().add("list",list);
    }
    /*
     * 分页查询
     * */
    @RequestMapping("/selectscienceAll2/{page}")
    public R selectScienceAll2(@PathVariable("page") int page,@RequestBody Science science){
        QueryWrapper<Science> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(science.getTitle()!="" && !("".equals(science.getTitle())),"title",science.getTitle());
        //第一个参数表示第几页，第二个参数表示每页的条数
        PageHelper.startPage(page,3);
        List<Science> list=this.scienceService.list(queryWrapper);
        PageInfo<Object> pageInfo = new PageInfo<>(list);
        return new R().add("pageInfo",pageInfo);
    }

    /*
     * 新增科普
     * */
    @PostMapping("/Addscience")
    public R AddScience(@RequestBody Science science){
        this.scienceService.save(science);
        return new R();
    }

    /*
     * 科普删除
     * */
    @RequestMapping("/Delscience/{id}")
    public R DelScience(@PathVariable("id") int id){
        boolean remove = this.scienceService.removeById(id);
        R r=new R();
        if(remove==true){
            r.setMessage("删除成功");
        }else{
            r.setMessage("删除失败");
        }
        return r;
    }

    /*
     * 根据科普id查询
     * */
    @PostMapping("/selectscience")
    public R selectScience(@RequestBody Science science){
        Science byId = this.scienceService.getById(science.getId());
        return new R().add("byId",byId);
    }

    /*
     * 修改科普
     * */
    @PostMapping("/uploadscience")
    public R uploadScience(@RequestBody Science science){
        this.scienceService.updateById(science);
        return new R();
    }
    /*
     * 图片上传
     * */
    @PostMapping("/uploadfile3")
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\HTML\\cat-html\\CAY\\img"+file.getOriginalFilename()));
        return new R().add("fname",file.getOriginalFilename());
    }

    /*
     * 图片上传
     * */
    @PostMapping("/uploadfile4")
    public R uploadfile4(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        file.transferTo(new File("D:\\HTML\\cat-html\\CAY\\img"+file.getOriginalFilename()));
        return new R().add("fname",file.getOriginalFilename());
    }
}
